import { err, to, toast, get } from "~/utils/wxapi";
import { dateTime } from "~/utils/util";
import { save } from "~/apis/order";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    info: "",
    address: "",
    latitude: 0,
    longitude: 0,
    orderDate: dateTime(new Date()),
    minDate: new Date().getTime(),
    maxDate: new Date(2024, 12, 31).getTime(),
    show: false,
    qrshow: false,
  },
  onLoad() {
    wx.getLocation({
      type: "wgs84",
      success: (res) => {
        const { latitude, longitude } = res;
        this.setData({ latitude, longitude });
      },
      fail: (err) => {
        console.log(err);
      },
    });
  },
  onShow() {
    if (typeof this.getTabBar === "function" && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0,
      });
    }
  },
  timeshow() {
    this.setData({ show: true });
  },
  onInput(e: any) {
    this.setData({ orderDate: dateTime(e.detail), show: false });
    console.log(this.data.orderDate);
  },
  gps() {
    const { latitude, longitude } = this.data;
    wx.chooseLocation({
      latitude,
      longitude,
      success: (res) => {
        console.log(res);
        this.setData({ address: res.address + res.name });
      },
      fail: (error) => {
        console.log(error);
        err(error);
      },
    });
  },
  onClose() {
    this.setData({ show: false, qrshow: false });
  },
  async sub() {
    try {
      const { orderDate, address, info } = this.data;
      const userId = get("user").id;
      if (!userId) return to("/pages/login/login");
      const res: any = await save({ orderDate, address, info, userId });
      console.log(res);
      if (res.code === 200) {
        toast(res.message);
        this.setData({ qrshow: true });
      } else {
        toast("预约失败");
      }
    } catch (error) {}
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
